<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <div id="app">
        <div>父组件</div>
        <button @click='handle'>销毁事件</button>
        <text-tom></text-tom>
        <text-jerry></text-jerry>
    </div>
    <script src="./vue.js"></script>
    <script>
        // 提供一个事件中心
        var hub = new Vue
        Vue.component('text-tom', {
            data: function() {
                return {
                    num: 0
                }
            },
            template: `
            <div>
                <div>TOM:{{num}}</div>
                <button @click='handle'>点击</button>

            </div>
            `,
            methods: {
                handle: function() {
                    hub.$emit('jerry-event', 2)
                }
            },
            mounted: function() {
                hub.$on('tom-event', (val) => {
                    this.num += val
                })
            }
        })
        Vue.component('text-jerry', {
            data: function() {
                return {
                    num: 0
                }
            },
            template: `
            <div>
                <div>JERRY:{{num}}</div>
                <button @click='handle'>点击</button>

            </div>
            `,
            methods: {
                handle: function() {
                    hub.$emit('tom-event', 10)
                }
            },
            mounted: function() {
                hub.$on('jerry-event', (val) => {
                    this.num += val
                })
            }
        })
        var vm = new Vue({
            el: '#app',
            data: {

            },
            methods: {
                handle: function() {
                    hub.$off('tom-event')
                    hub.$off('jerry-event')
                }
            }

        })
    </script>
</body>

</html>